<?php

namespace App\Http\Controllers\Api;

use App\DateReportStatus;
use App\OrderOriginal;
use App\SettlementReport;
use App\ShowtimeSchedule;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Session;


class StatisticalReportController extends BaseController
{
    public $user;

    public function __construct(Request $request)
    {
        parent::__construct();
        $this->user = $this->is_login($request);
    }

    /**
     * @param Request $request
     * @return array
     * 票房上报表
     */
    public function index(Request $request){

        $cinema_code = $this->user['data']['cinema_code'];
        $user_type = $this->user['data']['type'];

        $settlement_report_list = \DB::table('settlement_reports')
            ->join('films','films.code','=','settlement_reports.film_code')
            ->join('screens','screens.code','=','settlement_reports.screen_code');

        $business_start_date =   $request->input('business_start_date');
        $business_end_date =   $request->input('business_end_date');

        $business_end_date&&$business_start_date&&$settlement_report_list->whereBetween('business_date',[$business_start_date,$business_end_date]);
        $settlement_report_list->select('settlement_reports.*','screens.name as screen_name','films.name as film_name');
        $data = $settlement_report_list->orderBy('business_date','desc')->paginate(15);
        return success($data,'查询成功');
    }

    /**
     * @param Request $request
     * 综合票务表
     */
    public function order_original(Request $request){

        if (!$this->user['data']->can('synthetical_ticket_table')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        $user_type = $this->user['data']['type'];
        $business_start_date = $request->input('business_start_date');
        $business_end_date = $request->input('business_end_date');
        $film_name = $request->input('film_name');
        $screen_name = $request->input('screen_name');
        $channel = $request->input('channel');
        $start_datetime = $request->input('start_datetime');
        $end_datetime = $request->input('end_datetime');
        $cinema_name = $request->cinema_name;
        $order_original_list = \DB::table('order_originals')
            ->join('films','order_originals.film_code','=','films.code')
            ->join('users','order_originals.channel_id','=','users.id')
            ->join('cinemas','order_originals.cinema_code','=','cinemas.code')
            ->join('seats','seats.code','=','order_originals.seat_code')
            ->join('screens','screens.code','=','order_originals.screen_code')
            ->orderBy('date_time','desc')
            ->select('order_originals.*','films.name as film_name','cinemas.name as cinema_name','screens.name as screen_name','users.name as channel_name','seats.row_num as row_num','seats.column_num as column_num');
//            ->select('order_originals.*','cinemas.name as cinema_name','films.name as film_name','users.name as channel_name');
        $user_type==2&&$order_original_list->where('order_originals.cinema_code','=',$cinema_code);
        $user_type==1&&$cinema_name&&$order_original_list->where('cinema_name','like','%'.$cinema_name.'%');
        $business_start_date&&$business_end_date&&$order_original_list->whereBetween('order_originals.business_date',[$business_start_date,$business_end_date]);
        $film_name&&$order_original_list->where('films.name','like','%'.$film_name.'%');
        $screen_name&&$order_original_list->where('screens.name','like','%'.$screen_name.'%');
        $channel&&$order_original_list->where('users.name','like','%'.$channel.'%');
        $start_datetime&&$end_datetime&&$order_original_list->whereBetween('date_time',[$start_datetime,$end_datetime]);
        $data = $order_original_list->paginate(15);
        return success($data,'查询成功');
    }

    /**
     * @param Request $request
     * 管理员日志
     */
    public function get_logs(Request $request){

        if (!$this->user['data']->can('log_manage')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        $user_type = $this->user['data']['type'];
        $start_time = $request->start_time;
        $end_time = $request->end_time;
        $cinema_name = $request->cinema_name;
        $msg = $request->msg;

        $manager_log_list = \DB::table('manager_logs')
            ->orderBy('date_time','desc');
        $user_type==2&&$manager_log_list->where('cinema_code','=',$cinema_code);
        $msg&&$manager_log_list->where('msg','like','%'.$msg.'%');
        $start_time&&$end_time&&$manager_log_list->whereBetween('date_time',[$start_time,$end_time]);
        $user_type==1&&$cinema_name&&$manager_log_list->where('cinema_name','like','%'.$cinema_name.'%');
        $data = $manager_log_list->paginate(15);

        return success($data,'查询成功');
    }

    /**
     * @param Request $request
     * 添加、设置结算上报
     */
    public function add_settlement_reports(Request $request){


        $cinema_code = $this->user['data']['cinema_code'];
        if (!$business_date = $request->business_date){
            return error('营业日缺失');
        }

//        if ($business_date>=date('Y-m-d')){
//            return error('不能生成当天或未来天结算上报数据');
//        }

        $count = SettlementReport::where(['business_date'=>$business_date])->count();
        if ($count>0){
            return error('该营业日已生成结算上报数据');
        }

        $session_list = ShowtimeSchedule::where(['showtime_schedules.cinema_code'=>$cinema_code,'showtime_schedules.business_date'=>$business_date,'showtime_schedules.status'=>1])
            ->join('sessions','sessions.showtime_schedule_code','=','showtime_schedules.code')
            ->select('sessions.*')
            ->get();
        if ($session_list->count()==0){
            return error('不存在排片计划');
        }
        $order_original_list = OrderOriginal::where(['business_date'=>$business_date,'cinema_code'=>$cinema_code])->get();

        $data = [];
        foreach ($session_list as $val){
            $info = [];
            $info['business_date'] = $business_date;
            $info['cinema_code'] = $cinema_code;
            $info['session_code'] = $val['code'];
            $info['screen_code'] = $val['screen_code'];
            $info['film_code'] = $val['film_code'];
            $info['flag'] = $info['type'] = 1;
            $info['session_datetime'] = date('Y-m-d H:i:s',$val['start_time']);
            $info['local_sales_count'] = $info['local_refund_count'] = $info['online_sales_count'] = $info['online_refund_count']=$info['past_sales_count']=0;
            $info['local_sales'] = $info['local_refund'] = $info['online_sales'] = $info['online_refund']=$info['past_sales']=0;
            foreach ($order_original_list as $v){
                if ($val['code']==$v['session_code']){

                    if ($v['online_sale']==1){
                        switch ($v['operation']){
                            case 1:
                                $info['local_sales'] += $v['price'];
                                $info['local_sales_count'] += 1;
                                break;
                            case 2:
                                $info['local_refund'] +=$v['price'];
                                $info['local_refund_count'] +=1;
                                $info['local_sales'] -=$v['price'];
                                $info['local_sales_count'] -=1;
                                break;
                            case 3:
                                $info['local_sales'] +=$v['price'];
                                $info['local_sales_count'] +=1;
                                break;
                            case 4:
                                $info['past_sales_count'] +=1;
                                $info['past_sales'] += $v['price'];
                                $info['local_sales_count'] +=1;
                                $info['local_sales'] += $v['price'];
                                break;
                        }
                    }else{
                        switch ($v['operation']){
                            case 1:
                                $info['online_sales'] += $v['price'];
                                $info['online_sales_count'] += 1;
                                break;
                            case 2:
                                $info['online_refund'] +=$v['price'];
                                $info['online_refund_count'] +=1;
                                $info['online_sales'] -=$v['price'];
                                $info['online_sales_count'] -=1;
                                break;
                            case 3:
                                $info['online_sales'] +=$v['price'];
                                $info['online_sales_count'] +=1;
                                break;
                        }
                    }

                }
            }
            $data[] = $info;
        }
        if (\DB::table('settlement_reports')->insert($data)){
            return success('','添加成功');
        }else{
            return error('添加失败');
        }
    }

    /**
     * @param Request $request
     * 结算上报表
     */
    public function get_settlement_reports(Request $request){

        if (!$this->user['data']->can('settlement_report_table')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        $user_type = $this->user['data']['type'];

        $cinema_name = $request->cinema_name;
        $start_business_date = $request->start_business_date;
        $end_business_date = $request->end_business_date;
        $film_name = $request->film_name;
        $screen_name = $request->screen_name;

        $settlement_report_list = \DB::table('settlement_reports')
            ->join('cinemas','cinemas.code','=','settlement_reports.cinema_code')
            ->join('screens','screens.code','=','settlement_reports.screen_code')
            ->join('films','films.code','=','settlement_reports.film_code')
            ->orderBy('session_datetime','desc')
            ->select('settlement_reports.*','cinemas.name as cinema_name','screens.name as screen_name','films.name as film_name');
        $user_type==1&&$cinema_name&&$settlement_report_list->where('cinemas.name','like','%'.$cinema_name.'%');
        $user_type==2&&$settlement_report_list->where('settlement_reports.cinema_code','=',$cinema_code);
        $start_business_date&&$end_business_date&&$settlement_report_list->where('settlement_reports.business_date','>=',$start_business_date)->where('settlement_reports.business_date','<=',$end_business_date);
        $film_name&&$settlement_report_list->where('films.name','like','%'.$film_name.'%');
        $screen_name&&$settlement_report_list->where('screens.name','like','%'.$screen_name.'%');
        $data = $settlement_report_list->paginate(15);
        return success($data,'查询成功');
    }

    /**
     * @param Request $request
     * 按月查看上报状态
     */
    public function get_business_report_status(Request $request){
        if(!$years = trim($request->years)){
            return error('年份信息缺失');
        }

        if(!$months = trim($request->months)){
            return error('月份信息缺失');
        }

        if (!is_numeric($years)){
            return error('年份输入错误');
        }

        if (!is_numeric($months)&&$months>0&&$months<13){
            return error('月份输入错误');
        }

        $cinema_code = $this->user['data']['cinema_code'];

        $days = getMonthLastDay($months,$years);
        $data = [];
        for ($i=1;$i<=$days;$i++){
            $business_report = [];
            $business = Carbon::create($years,$months,$i,0,0,0)->toDateTimeString();
            $business_report_info = \DB::select('select * from date_report_status WHERE business_date="'.$business.'" and cinema_code='.$cinema_code.' order by date_time desc limit 1');
            if (count($business_report_info)>0){
                $business_report_info = $business_report_info[0];
                $business_report['business_date'] = $business_report_info->business_date;
                $business_report['flag'] = $business_report_info->flag;
                $business_report['type'] = $business_report_info->type;
            }else{
                $business_report['business_date'] = $business;
                $business_report['flag'] = $business_report['type'] = 0;
            }

            $data[] = $business_report;
        }
        return success($data,'查询成功');
    }

    /**
     * @param Request $request
     * 获取上报日志
     */
    public function get_report_logs(Request $request){
        $list = \DB::table('date_report_status');
        $start_business_date = $request->start_business_date;
        $end_business_date = $request->end_business_date;
        $start_business_date&&$end_business_date&&$list->whereBetween('business_date',[$start_business_date,$end_business_date]);
        $data = $list->paginate(15)->toArray();
        foreach ($data['data'] as $key=>$val){
            $data['data'][$key]->showtime_sechedule_list = ShowtimeSchedule::where('business_date','=',$val->business_date)->get();
        }
        return success($data,'获取上报日志成功');
    }

    /**
     * @param Request $request
     * 后台控制面板报表信息
     */
    public function statement_info(Request $request){

        $todays = date('Y-m-d');

        $weekends = Carbon::now()->subWeek(1)->format('Y-m-d');

        $months = Carbon::now()->subMonths(1)->format('Y-m-d');

        $years = Carbon::now()->subYears(1)->format('Y-m-d');

        $data = [];

        $local_todays_ticket_info = SettlementReport::where('business_date','>=',$todays)->sum('local_sales');
        $online_todays_ticket_info = SettlementReport::where('business_date','>=',$todays)->sum('online_sales');
        $local_weekends_ticket_info = SettlementReport::where('business_date','>=',$weekends)->sum('local_sales');
        $online_weekends_ticket_info = SettlementReport::where('business_date','>=',$weekends)->sum('online_sales');
        $local_months_ticket_info = SettlementReport::where('business_date','>=',$months)->sum('local_sales');
        $online_months_ticket_info = SettlementReport::where('business_date','>=',$months)->sum('online_sales');
        $local_years_ticket_info = SettlementReport::where('business_date','>=',$years)->sum('local_sales');
        $online_years_ticket_info = SettlementReport::where('business_date','>=',$years)->sum('online_sales');

        $data['todays'] = $local_todays_ticket_info+$online_todays_ticket_info;
        $data['weekends'] = $local_weekends_ticket_info+$online_weekends_ticket_info;
        $data['months'] = $local_months_ticket_info+$online_months_ticket_info;
        $data['years'] = $local_years_ticket_info+$online_years_ticket_info;

        return success($data,'查询成功');

    }













}
